/*
 * Copyright 2018 Zhongan.com All right reserved. This software is the
 * confidential and proprietary information of Zhongan.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Zhongan.com.
 */

package com.zhongan.cloud.gateway.route;

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;

//@Configuration
public class RouteLocators {

    @Bean
    public RouteLocator baiduRouteLocator(RouteLocatorBuilder builder) {
        //当访问/baidu时，跳转到https://www.zhongan.com
        return builder.routes().route(r -> r.path("/baidu").uri("http://www.zhongan.com").id("baiduRoute")).build();
    }

    @Bean
    public RouteLocator helloServiceRouteLocator(RouteLocatorBuilder builder) {
        return builder.routes()
                .route(spec -> spec.path("/hello-service/**")
                        //stripPrefix(1)：转发时去掉url中的第一段，即spec.path("/hello-service/**")中的/hello-service
                        .filters(f -> f.stripPrefix(1))
                        //uri("lb://HELLO-SERVICE")表示Spring Cloud Gateway对spring.application.name等于hello-service源服务应用中的URL进行协议适配转发。
                        .uri("lb://HELLO-SERVICE")
                        .order(0)
                        .id("test-hello-service"))
                .build();
    }

}
